﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ChainOfResp
{
    public abstract class LogHandler
    {

        protected string _regExp;
        protected LogHandler _successor;

        public abstract TaskStatus HandleRequest(string log);

        public void SetSuccessor(LogHandler logHandler)
        {
            _successor = logHandler;
        }
    }

    [Flags]
    public enum TaskStatus
    {
        None,
        Started,
        Finished,
        Error,    
    }
 
    public class TErrorHandler : LogHandler
    {
        public TErrorHandler()
        {
            _regExp = "Ma RegExp Error";
        }

        public override TaskStatus HandleRequest(string log)
        {
            var status = TaskStatus.None;

            if(_successor!=null)
                status = _successor.HandleRequest(log);

            Regex myRegex = new Regex(_regExp);
            if(myRegex.IsMatch(log))
            {
                status |= TaskStatus.Error;
            }
            return status;
        }
    }

    public class TFinishedHandler : LogHandler
    {
        public TFinishedHandler()
        {
            _regExp = "Ma RegExp Finished";
        }

        
        public override TaskStatus HandleRequest(string log)
        {
            var status = TaskStatus.None;

            if(_successor!=null)
                status = _successor.HandleRequest(log);

            Regex myRegex = new Regex(_regExp);
            if(myRegex.IsMatch(log) && log.StartsWith("S") )
            {
                status |= TaskStatus.Finished;
            }
            return status;
        }
    }

    public class TStartedHandler : LogHandler
    {
        public TStartedHandler()
        {
            _regExp = "Ma RegExp Started";
        }

        public override TaskStatus HandleRequest(string log)
        {
            var status = TaskStatus.None;

            if (_successor != null)
                status = _successor.HandleRequest(log);

            Regex myRegex = new Regex(_regExp);
            if (myRegex.IsMatch(log) && log.StartsWith("S"))
            {
                status |= TaskStatus.Started;
            }
            return status;
        }
    }
}
